package shuzi.util;

import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import shuzi.data.Database;
import shuzi.model.AfterSale;
import shuzi.model.Brand;
import shuzi.model.BrandPhone;
import shuzi.model.Dealer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Webserice {

	/** 命名空间 */
	public static final String NAMESPACE = "http://ws.it.tech.com/";
	/** 地址 */
	public static final String URL = "http://42.121.19.153:8888/bcm/ws/FetchDisplayInfoWS?wsdl";
	/** 获得所有品牌的方法名 */
	public static final String getBrandEntry = "getBrandEntry";
	/** 获得某一品牌手机的方法名 */
	public static final String getBrandList = "getBrandList";
	/** 获得推荐数据 */
	public static final String getRecmendData = "getRecmendData";
	/** 获得售后 */
	public static final String getServiceInfo = "getServiceInfo";
	/** 短信注册   */
	public static final String saveTermInfo = "saveTermInfo";

	/**
	 * 获取所有手机品牌的接口
	 * 
	 * @param handler
	 * @param typeId
	 * @return
	 */
	public static void getMovieDataList(Handler handler, int what) {
		String result = "";
		SoapObject rpc = new SoapObject(NAMESPACE, getBrandEntry);
		// rpc.addProperty("typeId", typeId);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// envelope.bodyOut = rpc;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		envelope.encodingStyle = "UTF-8";
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + getBrandEntry, envelope);
			if (envelope.getResponse() != null) {
				Object object = (Object) envelope.getResponse();
				Log.v(Database.TAG, "返回结果=" + object.toString());
				result = object.toString();
				List<Brand> list = getBrandTypeLists(result);
				// 如果
				if (list == null) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
				} else if (list.size() == 0) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_NODATA);
				} else {
					Message msg = new Message();
					msg.obj = list;
					msg.what = what;
					handler.sendMessage(msg);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
		}
		return;
	}

	/**
	 * 二级接口
	 * 
	 * @param typeId
	 * @param brandId
	 * @param name
	 * @param handler
	 * @param what
	 */
	public static void getPhoneList(String brandId, String name,
			Handler handler, int what) {
		String result = "";
		SoapObject rpc = new SoapObject(NAMESPACE, getBrandList);
		rpc.addProperty("orderFlag", "0");
		rpc.addProperty("brandId", brandId);
		rpc.addProperty("name", "");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// envelope.bodyOut = rpc;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		envelope.encodingStyle = "UTF-8";
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + getBrandList, envelope);
			if (envelope.getResponse() != null) {
				Object object = (Object) envelope.getResponse();
				Log.v(Database.TAG, "返回结果=" + object.toString());
				result = object.toString();
				List<BrandPhone> list = getBrandPhoneLists(result);
				// 如果
				if (list == null) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
				} else if (list.size() == 0) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_NODATA);
				} else {
					Message msg = new Message();
					msg.obj = list;
					msg.what = what;
					handler.sendMessage(msg);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
		}
		return;
	}

	/**
	 * 获取今日推荐的数据
	 * 
	 * @param handler
	 * @param what
	 */
	public static void getRecmendData(Handler handler, int what) {
		String result = "";
		SoapObject rpc = new SoapObject(NAMESPACE, getRecmendData);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		// envelope.bodyOut = rpc;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		envelope.encodingStyle = "UTF-8";
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + getRecmendData, envelope);
			if (envelope.getResponse() != null) {
				Object object = (Object) envelope.getResponse();
				Log.v(Database.TAG, "返回结果=" + object.toString());
				result = object.toString();
				List<BrandPhone> list = getBrandPhoneLists(result);
				// 如果
				if (list == null) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
				} else if (list.size() == 0) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_NODATA);
				} else {
					Message msg = new Message();
					msg.obj = list;
					msg.what = what;
					handler.sendMessage(msg);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
		}
		return;
	}

	/**
	 * 获取今日推荐的数据
	 * 
	 * @param handler
	 * @param what
	 */
	public static void getServiceInfo(String province, String city,
			String brandId, Handler handler, int what) {
		String result = "";
		SoapObject rpc = new SoapObject(NAMESPACE, getServiceInfo);
		rpc.addProperty("province", province);
		rpc.addProperty("city", city);
		rpc.addProperty("brandId", "2");
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// envelope.bodyOut = rpc;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		envelope.encodingStyle = "UTF-8";
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + getServiceInfo, envelope);
			if (envelope.getResponse() != null) {
				Object object = (Object) envelope.getResponse();
				Log.v(Database.TAG, "返回结果=" + object.toString());
				result = object.toString();
				List<AfterSale> list = getAfterSaleLists(result);
				// 如果
				if (list == null) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
				} else if (list.size() == 0) {
					handler.sendEmptyMessage(Database.HANDLER_REFRESH_NODATA);
				} else {
					Message msg = new Message();
					msg.obj = list;
					msg.what = what;
					handler.sendMessage(msg);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			handler.sendEmptyMessage(Database.HANDLER_REFRESH_ERROR);
		}
		return;
	}

	/**
	 * 获取短信信息的接口
	 * @param termType
	 * @param phoneMacAdress
	 * @param phoneNumber
	 * @param dealerContactNumber
	 * @param deliveryAddress
	 * @param handler
	 * @param what
	 * @return 
	 */
	public static String saveTermInfo( String phoneMacAdress,
			String phoneNumber, String dealerContactNumber,
			String deliveryAddress) {
		String result = "";
		SoapObject rpc = new SoapObject(NAMESPACE, saveTermInfo);
		rpc.addProperty("termType", "1");
		rpc.addProperty("phoneMacAdress", phoneMacAdress);
		rpc.addProperty("phoneNumber", phoneNumber);
		rpc.addProperty("dealerContactNumber", dealerContactNumber);
		rpc.addProperty("deliveryAddress", deliveryAddress);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// envelope.bodyOut = rpc;
		envelope.dotNet = false;
		envelope.setOutputSoapObject(rpc);
		envelope.encodingStyle = "UTF-8";
		HttpTransportSE ht = new HttpTransportSE(URL);
		try {
			ht.call(NAMESPACE + saveTermInfo, envelope);
			if (envelope.getResponse() != null) {
				Object object = (Object) envelope.getResponse();
				Log.v(Database.TAG, "返回结果=" + object.toString());
				result = object.toString();
			
			

			}
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
		}
		return result;
	}

	/**
	 * 解析所有品牌类别
	 * 
	 * @param json
	 */
	public static List<Brand> getBrandTypeLists(String json) {
		List<Brand> list = null;
		Type listType = new TypeToken<LinkedList<Brand>>() {
		}.getType();
		Gson gson = new Gson();
		list = gson.fromJson(json, listType);

		return list;

		// System.out.println("======"+Database.MovieTypeLists.size());

	}

	/**
	 * 解析所有品牌手机
	 * 
	 * @param json
	 */
	public static List<BrandPhone> getBrandPhoneLists(String json) {
		List<BrandPhone> list = null;
		Type listType = new TypeToken<LinkedList<BrandPhone>>() {
		}.getType();
		Gson gson = new Gson();
		list = gson.fromJson(json, listType);

		return list;

		// System.out.println("======"+Database.MovieTypeLists.size());

	}

	/**
	 * 解析所有售后
	 * 
	 * @param json
	 */
	public static List<AfterSale> getAfterSaleLists(String json) {
		List<AfterSale> list = null;
		Type listType = new TypeToken<LinkedList<AfterSale>>() {
		}.getType();
		Gson gson = new Gson();
		list = gson.fromJson(json, listType);

		return list;

		// System.out.println("======"+Database.MovieTypeLists.size());

	}
	
	/**
	 * 是否注册成功
	 * @param jsonText
	 * @return
	 */
	public static boolean  isRegisterSuccess(String jsonText){
		boolean success = false;
		//如果返回的结果是空的话，注册失败
		if(jsonText==null||jsonText.equals("")){
			return success;
		}
		else{
			try {
				JSONObject obj = new JSONObject(jsonText);
				String str = obj.getString("data");
				JSONObject obj2 = new JSONObject(str);
				String str2 = obj2.getString("result");
				if(str2.equals("1")){					
					Gson gson = new Gson();
					Database.dealer =	gson.fromJson(obj2.getString("info"), Dealer.class);
					success = true;
				}else{
					return false;
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
			
		}
		
		return success;
		
	}

}
